var util = require('../../utils/util')
var cartBehavior = require('../../common/cartBehavior.js')
Page({
  behaviors: [cartBehavior],
  data: {
    page: 1,
    folowRequest: false,
    cateAdding: false,
  },

  toGoodDetail(e) {
    //将商品goods_id 传递给全局参数
    // console.log(e.currentTarget.dataset.good.goods_id)
    getApp().globalData.enterGoodDetialID = e.currentTarget.dataset.good.goods_id
    wx.navigateTo({
      url: '/pages/goodDetail/goodDetail',
    })
  },

  changFollow() {
    if (!this.data.folowRequest) {
      this.setData({
        folowRequest: true
      })
      let data = {
        shop_id: this.data.shopInfo.shop_id,
      }
      util.sendRequest('shop/collect', 'POST', data).then(res => {
        // console.log(res)
        if (res.data.code === 0) {
          let msg = res.data.msg;
          if (msg == "取消收藏成功") {
            this.setData({
              'shopInfo.collect_count': this.data.shopInfo.collect_count - 1,
              'shopInfo.is_collect': 2
            })
          } else if (msg == "收藏成功") {
            this.setData({
              'shopInfo.collect_count': this.data.shopInfo.collect_count + 1,
              'shopInfo.is_collect': 1
            })
          }
          wx.showToast({
            title: msg,
            icon: 'none',
            success: (res) => {
              this.setData({
                folowRequest: false
              })
            }
          })
        } else {
          wx.showToast({
            title: '请求失败，请稍后再尝试',
            icon: 'none',
          })
        }
      })
    } else {
      wx.showToast({
        title: '操作过快，请稍后...',
        icon: 'none',
      })
    }
  },


  toShopDetail() {
    wx.navigateTo({
      url: '/pages/shopDesc/shopDesc',
    })
  },

  //request店铺信息
  getShopInfo() {
    let data = getApp().globalData.enterShopInfo;
    // console.log(data)
    util.sendRequest('shop/home', 'GET', data).then(res => {
      // console.log(res)
      if (res.data.code === 0) {
        let result = res.data.result;
        //将头图字符串根据 , 转换成对象：
        let imgStr = result.door_head_img + '';
        let arr = imgStr.split(',');
        this.setData({
          shopInfo: result,
          doorImgArr: arr
        })
        // console.log(result)
      } else if (res.data.code === 1) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '获取店铺信息失败，请检查网络',
          icon: 'none'
        })
      }
    })
  },

  getShopGoods(page) {
    let shop_id = getApp().globalData.enterShopInfo.shop_id
    let data = {
      shop_id,
      limit: 8,
      page: page ? page : this.data.page
    }
    util.sendRequest('goods/list', 'GET', data).then(res => {
      console.log(res)
      if (res.data.code === 0) {
        let result = res.data.result;
        if (this.data.page === 1) {
          var shopGoods = []
        } else {
          var shopGoods = this.data.shopGoods
        }
        this.setData({
          'shopGoods': shopGoods.concat(result.data),
          'page': this.data.page + 1
        })
        console.log(this.data.shopGoods)
      }
    })
  },

  onLoad: function (options) {
    var app = getApp()
    this.setData({
      hoverHeight: app.globalData.windowHeight - app.globalData.navHeight,
      navHeight: app.globalData.navHeight,
      urls: app.globalData.urls
    })
    this.getShopInfo()
    this.getShopGoods();
    this.getCart()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCart();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})